# coding=utf-8

import datetime
import time

ALL_INFO = "%Y-%m-%d %H:%M:%S"
ONLY_DATE = "%Y-%m-%d"
ONLY_TIME = "%H:%M:%S"


def date_time_cal(my_time, days=0, hours=0, minutes=0, seconds=0) :
    """
    时间格式 计算
    :param my_time:
    :param days:
    :param hours:
    :param minutes:
    :param seconds:
    :return: 时间格式
    """
    return my_time + datetime.timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)

def date_time_cal_str(my_time, days=0, hours=0, minutes=0, seconds=0, format=ALL_INFO) :
    date_time = date_time_cal(my_time, days=days, hours=hours, minutes=minutes, seconds=seconds)
    return date_format(date_time, format=format)

def get_now_time():
    """
    获取当前时间
    :return: 时间格式
    """
    return datetime.datetime.now()

def get_now_time_str(format=ALL_INFO) :
    """
    获取当前时间
    :param format:
    :return: 字符串
    """
    now_time = get_now_time()
    return date_format(now_time, format=format)


def date_format(temp_time, format=ALL_INFO) :
    """
    时间格式化
    :param temp_time:
    :param format:
    :return:
    """
    return temp_time.strftime(format)


def str_to_timestamp(temp_time, format=ALL_INFO):
    """
    时间格式字符串，转为时间戳
    :param temp_time:
    :param format:
    :return:
    """
    timeStruct = time.strptime(temp_time, format)
    return int(time.mktime(timeStruct))

def timestamp_to_str(timeStamp, format=ALL_INFO) :
    # localTime = datetime.datetime.utcfromtimestamp(timeStamp)
    # return localTime.strftime(format)
    localTime = time.localtime(timeStamp)
    return time.strftime(format, localTime)


def str_to_date(date_str, format=ALL_INFO):
    return datetime.datetime.strptime(date_str, format)